package com.cy.pj.sys.service.impl;

import com.cy.pj.common.exception.ServiceException;
import com.cy.pj.sys.dao.SysUserDao;
import com.cy.pj.sys.dao.SysUserRoleDao;
import com.cy.pj.sys.pojo.SysUser;
import com.cy.pj.sys.service.SysUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private SysUserRoleDao sysUserRoleDao;

    @Override
    public int updatePassword(String sourcePassword, String newPassword, String confirmPassword) {
        //1.参数校验
        if(sourcePassword==null||"".equals(sourcePassword))
            throw new IllegalArgumentException("原密码不能为空");
        if(newPassword==null||"".equals(newPassword))
            throw new IllegalArgumentException("新密码不能为空");
        if(!newPassword.equals(confirmPassword))
            throw new IllegalArgumentException("两次输入的新密码不一致");
        SysUser user= (SysUser) SecurityUtils.getSubject().getPrincipal();
        SimpleHash sh=new SimpleHash(newPassword, sourcePassword, user.getSalt(), 1);
        if (!user.getPassword().equals(sh.toHex()))
            throw new IllegalArgumentException("原密码不正确");
        String newSalt=UUID.randomUUID().toString();
        sh=new SimpleHash("MD5",newPassword,newSalt,1);
        String newHashedPassword=sh.toHex();
        int rows=sysUserDao.updatePassword(newHashedPassword, newSalt,user.getId());
        return rows;
    }

    @Override
    public List<SysUser> selectUsers(SysUser sysUser) {
        return sysUserDao.selectUsers(sysUser);
    }

    @Override
    public SysUser selectById(Integer id) {
        //基于id查询用户以及用户对应部门信息
        SysUser user=sysUserDao.selectById(id);
        if(user==null)
            throw new ServiceException("此用户可能已经不存在");
        //基于id查询用户对应的角色id
        List<Integer> roleIds=sysUserRoleDao.selectRoleIdsByUserId(id);
        user.setRoleIds(roleIds);
        return user;
    }

    @Transactional
    @Override
    public int saveUser(SysUser sysUser) {
        //1.保存用户自身信息
        //对密码进行MD5加密(MD5是一种不可逆的加密算法，只能加密不能解密，相同内容加密结果也相同)
        String password=sysUser.getPassword();
        String salt= UUID.randomUUID().toString();
        SimpleHash simpleHash=
                new SimpleHash("MD5", password, salt, 1);
        password=simpleHash.toHex();
        sysUser.setPassword(password);
        sysUser.setSalt(salt);
        int rows=sysUserDao.insertUser(sysUser);
        //保存用户角色关系数据
        sysUserRoleDao.insertUserRoles(sysUser.getId(), sysUser.getRoleIds());
        return rows;
    }

    @Transactional
    @Override
    public int updateUser(SysUser sysUser) {
        int rows=sysUserDao.upDateUser(sysUser);
        if (rows==0)throw new ServiceException("记录可能已经不存在");
        sysUserRoleDao.deleteByUserId(sysUser.getId());
        sysUserRoleDao.insertUserRoles(sysUser.getId(), sysUser.getRoleIds());
        return rows;
    }

    @Transactional
    @Override
    public int validById(Integer id, Integer valid) {
        String modifiedUser="admin";
        int rows=sysUserDao.validById(id,valid, modifiedUser);
        if (rows==0)throw new ServiceException("记录可能已经不存在");
        return rows;
    }
}
